from app import db

from app.common.enums import IpFilterType
from .base_model import BaseModel


class IpFilter(BaseModel):
    """IP过滤器，请求进入时过滤白名单和黑名单IP
    """
    __tablename__ = 'ip_filter'
    __table_args__ = ({'comment': 'ip过滤器'})
    id = db.Column(db.Integer, primary_key=True, comment="记录ID")
    filter_type = db.Column(db.Enum(IpFilterType), server_default='black', nullable=False, comment="过滤类型")
    network_segment = db.Column(db.String(255), nullable=False, comment="ip网段")
    is_enabled = db.Column(db.Boolean, default=True, nullable=False, comment="启用")
    description = db.Column(db.String(255), nullable=True, comment="描述")

    def __repr__(self) -> str:
        return "<IpFilter %s>" % self.to_json()
